<html>
  <script src="https://cdn.jsdelivr.net/npm/@ruby/head-wasm-wasi@2.3.0/dist/browser.script.iife.js"></script>
  <script type="text/ruby" data-eval="async">
    # Patch require_relative to load from remote
    require 'js/require_remote'

    module Kernel
      alias original_require_relative require_relative

      # The require_relative may be used in the embedded Gem.
      # First try to load from the built-in filesystem, and if that fails,
      # load from the URL.
      def require_relative(path)
        caller_path = caller_locations(1, 1).first.absolute_path || ''
        dir = File.dirname(caller_path)
        file = File.absolute_path(path, dir)

        original_require_relative(file)
      rescue LoadError
        JS::RequireRemote.instance.load(path)
      end
    end

    # The above patch should not break the original require_relative
    require 'uri'
    URI.parse('http://example.com')

    # Load the main script
    require_relative 'main'
  </script>
</html>
